package com.flying.dcss.userpermissions.action;

import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;

import com.flying.dcss.app.SessionName;
import com.flying.dcss.userpermissions.bo.impl.DcssUser;
import com.flying.dcss.userpermissions.service.impl.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyPhoneNumAction extends ActionSupport {

	private UserService userService;

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String phoneNum = ServletActionContext.getRequest().getParameter(
				"phoneNum");

		DcssUser user = (DcssUser) ServletActionContext.getRequest()
				.getSession().getAttribute(SessionName.UserSession);

		if (user == null) {
			// Message.
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Please login!');top.location='login.jsp';</script>");
			out.flush();
			out.close();
			return this.ERROR;
		} else {
			DcssUser realUser = userService.getUserById(user.getId());
			
			realUser.setPhoneNum(phoneNum);
			userService.modifyUser(realUser);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			out.print("<script>alert('Succeed!');top.location='index.jsp';</script>");
			out.flush();
			out.close();
			return this.SUCCESS;
		}
	}
}
